package com.suixun.thread;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;

import com.suixun.excel.model.ExcelErrorRecord;
import com.suixun.excel.model.StudentModel;
import com.suixun.service.DeviceService;

public class BatchImportCardThread extends Thread {
	private CountDownLatch latch;
	private StudentModel student;
	private int index;

	public BatchImportCardThread(CountDownLatch latch, StudentModel student, int index) {
		this.latch = latch;
		this.student = student;
		this.index = index;
	}

	protected static Logger logger = LoggerFactory.getLogger(BatchImportCardThread.class);

	@Override
	public void run() {
		DeviceService deviceService = ContextLoader.getCurrentWebApplicationContext().getBean(DeviceService.class);
		try {
			deviceService.addStudent(student, index);
		} catch (Exception e) {
			ExcelErrorRecord record = new ExcelErrorRecord();
			record.setCell("");
			record.setRemark("出现错误，请联系管理员解决");
			record.setRow(String.valueOf(index + 1));
			student.setRecord(record);
			logger.error("batchImportCardThreadError:" + e.getMessage(), e);
		}finally {
			latch.countDown();
		}
	}
}
